package com.yunjing.elasticjob.service;


import com.yunjing.elasticjob.entity.JobConfig;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Trigger;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;
import org.springframework.stereotype.Component;

import java.text.ParseException;

/**
 * @author rql
 * @version 1.0.0
 * @date 2018/3/26 19:18
 * @description
 */
@Slf4j
@Component
public class TriggerBuilder {


    public Trigger build(JobConfig jobConfig) {
        CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
        factoryBean.setName(jobConfig.getName());
        factoryBean.setGroup(jobConfig.getGroupName());
        String cron = jobConfig.getCron();
        log.info("*****************************" + cron);
        factoryBean.setCronExpression(cron);
        try {
            factoryBean.afterPropertiesSet();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return factoryBean.getObject();
    }


    public Trigger simpleBuild(JobConfig jobConfig) {
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setName(jobConfig.getName());
        factoryBean.setGroup(jobConfig.getGroupName());
        factoryBean.setRepeatCount(0);
        factoryBean.afterPropertiesSet();
        return factoryBean.getObject();
    }


}
